namespace :bld do
  desc "Get data"
  task :get_acr_data => :environment do
    RAILS_DEFAULT_LOGGER.fatal "Start #{Time.now}"
    date_now =  Time.now
    ac_types = ENV['codes'] ? AcType.find_all_by_page_code(ENV['codes'].split(',')) : AcType.find_all_by_code_type(0)
    scan = Scan.create! :description => "#{DateTime.now} 创建的记录", :status => 0
    begin
      scan.search ac_types
	  scan.update_attribute :status, 1
    rescue
      RAILS_DEFAULT_LOGGER.error "#{$!}"
      RAILS_DEFAULT_LOGGER.error "#{$@}"
      p $!,$@
	  scan.update_attribute :status, 3
      raise
    end
    puts "#{Time.now - date_now} second complete"
    if scan.status == 1
      date_now =  Time.now
      puts "processing..."
      RAILS_DEFAULT_LOGGER.fatal "processing..."
      scan.result_process
      puts "#{Time.now - date_now} second complete"
      RAILS_DEFAULT_LOGGER.fatal "#{Time.now - date_now} second complete"
    end
  end

  desc "Get single info"
  task :get_work_info => :environment do
    works = Work.find :all, :conditions => "#{'part_count is null and ' unless ENV['all']} status = 1", :order => "wid"
      works.each do |work|
        print "|==#{work.wid.to_s}:"
        work.get_info
        print %<  #{work.part_count}p\n>
      end
  end

  desc "ranking work info fixed"
  task :ranking_work_info_fixed => :environment do
    puts "=====begin to ranking fixed====="
    date_now =  Time.now
    id1 = ENV['id1'] || (YAML.load(open(File.join "#{RAILS_ROOT}/config/last.yml")) rescue 0)
    id2 = ENV['id2'] || Scan.last.id
    results = WeeklyResult.stat(id1, id2, [-1])
    #Point序
    #results.sort_by{|r| r[:comment_rank]}.each_with_index do |r, i|
     # r_f = Result.find r.id
      #r_f.comment_fixed
      #break if i == 5#只修复粗略计算的前多少名
    #end
    results.each_with_index do |r, i|
      print "#{r.work.wid}:"
      r.work.get_info
      sleep 0.5
      puts "all"
      break if i == 200#只修复粗略计算的前多少名
    end
    puts "#{Time.now - date_now} second complete"
  end

  desc "Get author only"
  task :get_authors => :environment do
    works = Work.find :all, :conditions => "status = 1 and author_id is null", :order => "wid"
      works.each do |work|
        print "|==wid:" + work.wid.to_s
        work.get_author_type_info
        print "  " + work.author_id.to_s + "\n"
      end
  end

end
